﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using Microsoft.Phone.Maps.Toolkit;
using Microsoft.Phone.Maps.Controls;
using System.Device.Location;
using SpaceNeedle.x6473.WindowsPhone.ViewModels;
using SpaceNeedle.x6473.WindowsPhone.Views;
using SpaceNeedle.x6473.WindowsPhone.Common;
using Windows.Networking.Connectivity;
using System.Globalization;
using SpaceNeedle.x6473.WindowsPhone.Models;
using SpaceNeedle.SDK.WindowsPhone.Views;
using SpaceNeedle.SDK.WindowsPhone;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;

namespace SpaceNeedle.x6473.WindowsPhone.Views
{
    public class CallsView : ViewBase<CallsViewModel> { }

    public partial class Calls : CallsView
    {
        public Calls()
        {
            InitializeComponent();

            this.Loaded += Calls_Loaded;
            this.ViewModel.Loaded += ViewModel_Loaded;
        }

        async void ViewModel_Loaded()
        {
            var coord = await AppServices.Current.LocationService.GetGeopositionAsync();

            this.map.Center = new GeoCoordinate(coord.Coordinate.Latitude.Value, coord.Coordinate.Longitude.Value);
            this.map.ZoomLevel = 14;

            if (this.ViewModel.RequestItems == null)
            {
                return;
            }

            foreach (var item in this.ViewModel.RequestItems)
            {
                if (string.IsNullOrEmpty(item.Geoposition))
                {
                    continue;
                }

                MapLayer layer = new MapLayer();
                Pushpin pushpin = new Pushpin();

                //pushpin.Content = new Image() { Source = new BitmapImage(new Uri(item.RequestIcon)), Width = 48 };

                StackPanel stack = new StackPanel();
                stack.Orientation = System.Windows.Controls.Orientation.Horizontal;

                Image pushpinIcon = new Image() { Source = new BitmapImage(new Uri(item.RequestIcon)), Width = 48 };
                stack.Children.Add(pushpinIcon);

                TextBlock textBlock = new TextBlock();
                textBlock.Text = item.TotalSupportersCount.ToString();
                stack.Children.Add(textBlock);
                
                pushpin.Content = stack;
                
                //pushpin.Content = new Image() { Source = new BitmapImage(new Uri(item.RequestIcon)), Width = 48 };
                pushpin.Tap += pushpin_Tap;
                pushpin.Tag = item;

                MapOverlay overlay = new MapOverlay();
                overlay.Content = pushpin;

                string[] parts = item.Geoposition.Split(',');

                if (parts.Length == 2)
                {
                    overlay.GeoCoordinate = new GeoCoordinate(ConvertToDouble(parts[0]), ConvertToDouble(parts[1]));
                    layer.Add(overlay);
                    map.Layers.Add(layer);
                }
            }
        }

        void pushpin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Pushpin pushpin = sender as Pushpin;

            RequestItem item = pushpin.Tag as RequestItem;

            AppViewModel.Instance.Properties.SelectedRequestItem = item;
            AppServices.Current.NavigationService.NavigateDirect(ViewsUris.RequestItemDetails);
        }

        private double ConvertToDouble(string number)
        {
            number = number.ToString().Replace(",", ".");

            if (string.IsNullOrEmpty(number))
            {
                return 0;
            }

            NumberFormatInfo provider = new NumberFormatInfo();
            provider.NumberDecimalSeparator = ".";
            provider.NumberGroupSeparator = ",";

            return System.Convert.ToDouble(number, provider);
        }

        void Calls_Loaded(object sender, RoutedEventArgs e)
        {
            ApplicationBar = ((ApplicationBar)this.Resources["appBar2"]);
        }

        private void OnFiltersAppBarButton_Click(object sender, EventArgs e)
        {
            //pivot.SelectedIndex = 0;
        }

        private void OnListAppBarButton_Click(object sender, EventArgs e)
        {
           //pivot.SelectedIndex = 0;

            this.pivotContainer.Visibility = System.Windows.Visibility.Visible;
            this.mapContainer.Visibility = System.Windows.Visibility.Collapsed;

            ApplicationBar = ((ApplicationBar)this.Resources["appBar2"]);
        }

        private void OnMapAppBarButton_Click(object sender, EventArgs e)
        {
            //pivot.SelectedIndex = 1;
            this.pivotContainer.Visibility = System.Windows.Visibility.Collapsed;
            this.mapContainer.Visibility = System.Windows.Visibility.Visible;

            ApplicationBar = ((ApplicationBar)this.Resources["appBar3"]);
        }

        private void OnPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if (pivot.SelectedIndex == 0)
            //{
            //    ApplicationBar = ((ApplicationBar)this.Resources["appBar1"]);
            //}
            //else if (pivot.SelectedIndex == 1)
            //{
            //    ApplicationBar = ((ApplicationBar)this.Resources["appBar2"]);
            //}
            //else if (pivot.SelectedIndex == 2)
            //{
            //    ApplicationBar = ((ApplicationBar)this.Resources["appBar3"]);
            //}

            //this.map.Visibility = this.pivot.SelectedIndex == 0 ? Visibility.Collapsed : Visibility.Visible;
            //this.map.Visibility = this.pivot.SelectedIndex == 1 ? Visibility.Visible : Visibility.Collapsed;

            //if (pivot.SelectedIndex == 0)
            //{
            //    this.pivotContainer.Visibility = System.Windows.Visibility.Visible;
            //    this.mapContainer.Visibility = System.Windows.Visibility.Collapsed;

            //    ApplicationBar = ((ApplicationBar)this.Resources["appBar2"]);
            //}
            //else if (pivot.SelectedIndex == 1)
            //{
            //    this.pivotContainer.Visibility = System.Windows.Visibility.Collapsed;
            //    this.mapContainer.Visibility = System.Windows.Visibility.Visible;

            //    ApplicationBar = ((ApplicationBar)this.Resources["appBar3"]);
            //}
        }

        private void OnRequestItemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
            {
                return;
            }

            AppViewModel.Instance.Properties.SelectedRequestItem = e.AddedItems[0] as Models.RequestItem;
            AppServices.Current.NavigationService.NavigateDirect(ViewsUris.RequestItemDetails);
        }

        private void OnShareTicket_Click(object sender, RoutedEventArgs e)
        {
            RequestItem item = (sender as MenuItem).Tag as RequestItem;

            ShareLinkTask shareLinkTask = new ShareLinkTask();
            shareLinkTask.LinkUri = new Uri(item.Permalink, UriKind.Absolute);
            shareLinkTask.Message = item.Description;

            shareLinkTask.Show();
        }

        private void OnUploadsMenuItem_Click(object sender, EventArgs e)
        {
            this.ViewModel.UploadsCommand.Execute(null);
        }

        private void OnAboutMenuItem_Click(object sender, EventArgs e)
        {
            this.ViewModel.AboutCommand.Execute(null);
        }

        private void OnSignOutMenuItem_Click(object sender, EventArgs e)
        {
            this.ViewModel.SignOutCommand.Execute(null);
        }
    }
}